<?php
/**
 * Created by PhpStorm.
 * User: mark
 * Date: 2019/9/29
 * Time: 10:30
 */

namespace app\web\controller;

use app\api\controller\v2\Auth;
use app\common\cachemodel\VipTask;
use app\common\cachemodel\ZlCommonProblems;
use app\common\cachemodel\ZlUserStarNumRecord;
use app\common\cachemodel\ZlUserVip;
use app\common\cachemodel\ZlVip;
use app\common\lib\exception\ApiException;
use app\common\lib\Util;
use app\common\model\User;
use think\Controller;
use think\Env;
use think\Exception;

class Vip extends Controller
{
    private $oss = '';
    private $domain = '';

    protected function _initialize(): void
    {
        parent::_initialize();
        $this->oss = Env::get('host.photo');
        $this->domain = Env::get('host.domain');
        $res = Util::androidVersion('2.2.0');
        $this->assign('version', $res);
    }

    /**
     * @return mixed|string
     * @throws \Exception
     */
    public function vip_center()
    {
        $isTest = false;//测试

        // 'vip_center?from=参数'（会员中心【from可以为空，默认为2《from=1（已开通/已过期的页面）：我的-“会员中心”按钮点击 或者 我的-顶部vip等级小图标点击 或者 订单详情页面的vip订单跳转 》；《from=2（立即购买的页面）：包含 会员升级任务的会员中心页面-点击“立即激活”或者“立即续费”按钮 或者 vip订单详情页面的再次购买按钮 】）
        $from = input('from/d', 2);
        //ios是否是全面屏，0：不是；1：是；
        $isAllScreen = input('isAllScreen/d', 0);

        //状态栏的高度，安卓使用
        $h_of_android_status_bar = input('h/d', 0);

        //获取后台录入的:VIP等级/会员名称/基础星力值/成长倍速
        $vips = (new ZlVip())->getLevelAll();
        $isAppleCheck = false; // 苹果审核模式 开启状态
        $auth = [];
        $token = $this->request->header('access-token');
        $app_type = $this->request->header('app-type');
        if (!$isTest)
        {
            //校验token
            try
            {
                if ($token)
                {//已登录
                    $auth = new Auth();
                    $auth->checkRequestAuth();
                    $isAppleCheck = $auth->is_apple_check;
                }
            }
            catch (Exception $e)
            {
                throw new ApiException('vip中心h5，错误：' . '权限认证失败,请稍后重试！');
            }

            //校验传入from参数
            if (0 !== $from && 1 !== $from && 2 !== $from)
            {
                throw new ApiException('vip中心h5，错误：' . 'from参数错误,请稍后重试！');
            }
        }

        try
        {
            if ($isTest)
            {//测试
//                $app_type = 'ios';
                $app_type = 'android';
//                $app_type = '';
                $vip = 1;
                //$status：0：未开通；1：已开通，未过期；2：已过期；
                $status = 0;
                $from = input('from/d', 2);
//                $isAllScreen = 0;
//                $userVip = false;
                $userVip = [
                    'is_valid' => 0,
                    'vip_level' => 1,
                    'star_num' => 100,
                    'end_time' => 1573710868
                ];
                $user = [
                    'id' => 10254602,
                    'uen' => '1576968259',
                    'token' => '574a5464b955d36bf905a717cd39d71812340da4',
                    'token_expire' => 1575977100,
                    'tel' => '17333657763',
                    'uc_id' => '7d19f0b9adb84e54d04d98b75a0786a9',
                    'nickname' => '哈哈哈哈哈哈和和和啊',
                    'sex' => 2,
                    'avatar' => 'uploads/avatar/20190703/1e0a5fa0253947e8411129c34892d620.png',
                    'status' => 1,
                    'has_self_recode' => 1,
                    'forbidden' => 0,
                    'regid' => '190e35f7e0127d67aff',
                    'vip' => $vip,
                ];
            } else
            {
                //没有登录只能进入到 开通会员页面
                if ($token)
                {//token存在
                    //查询用户vip等级和当前星力值
                    //---not test----
                    $headers = $auth->headers;
                    //header里的手机类型：“android”或者“ios”
//                    $app_type = $headers['app-type'];
                    $user = $auth->user;
                    $userVip = (new ZlUserVip())->getInfoById($user['id'], ['is_valid', 'vip_level', 'star_num', 'end_time', 'apple_status']);
                    //vip等级1~9
                    $vip = 1;

                    if ($userVip)
                    {
                        $status = 1 === (int)$userVip['is_valid'] ? 1 : 2;
                        $vip = (int)$userVip['vip_level'];
                    } else
                    {
                        //vip表中没有记录，说明从来没有订购过
                        $status = 0;
                    }
                } else
                {//没有登录
                    $status = 0;
                    $user = [
                        'id' => 0,
                        'nickname' => '用户',
                        'avatar' => '',
                        'tel' => '',
                    ];
                    $vip = 1;
                }

            }


            //用户是否绑定了手机号
            $is_bind_tel = 0;
            if (isset($user['tel']) && count($user['tel']) > 0)
            {
                $is_bind_tel = 1;
            }
            //用户信息部分
            $user = [
                'id' => $user['id'],
                'name' => $user['nickname'],
                'vip' => $vip,//当前vip等级
                'avatar' => $user['avatar'],
                'star_num' => isset($userVip['star_num']) ? $userVip['star_num'] : '',//当前的星力值
                'grow_num' => count($vips) > 0 ? $vips[$vip - 1]['grow_num'] : '',//星力值增长倍数
                'status' => $status,//$status：0：未开通；1：已开通，未过期；2：已过期；
                'vip_end_time' => isset($userVip['end_time']) ? date('Y-m-d', $userVip['end_time']) : ''//过期时间
            ];

            if ((1 === $from && $status !== 0) || (1 === $status && 0 === $from))
            {//vip中心模版
                $zlUserStarNumRecord = new ZlUserStarNumRecord();
                $star_num_cur_month = $zlUserStarNumRecord->getStarNumMonthlyLimit($user['id']);
                $star_num_override_per = $zlUserStarNumRecord->getUserPercentOver($user['id']);
                $jobs = [
                    'current' => [
                        'month' => date('m'),
                        'star_num_cur_month' => max($star_num_cur_month, 0),//本月获得的星力值
                        'star_num_max_month' => '5000',
                        'star_num_override_per' => $star_num_override_per,//“超过的用户”百分比
                        'target' => 'local',
                        'link' => 'pay_order?type=8',
                    ],
                ];

                //获取所有会员任务
                $tasks = (new VipTask())->getVipTask();
                foreach ($tasks as $k => $v)
                {
                    if ($v['name'] === '绑定联系方式')
                    {
                        $v['is_show'] = !$is_bind_tel;
                        $tasks[$k] = $v;
                    } else if ($v['name'] === '解锁一生报告')
                    {
                        if (Util::CheckAppVersion('1.9.6') === '小')
                        {
                            //老版本不显示“解锁本命预测”的任务
                            unset($tasks[$k]);
                        }
                    } else if ($v['name'] === '教学视频')
                    {//隐藏 教学视频 任务
                        unset($tasks[$k]);
                    }
                }

                //会员权益
                $right_list = [];
                for ($i = 1, $iMax = count($vips); $i <= $iMax; $i++)
                {
                    $item = [
                        [
                            'title' => '身份铭牌',
                            'des' => 'V' . $i . '小行星'
                        ],
                        [
                            'title' => $vips[$i - 1]['grow_num'] . '倍成长',
                            'des' => '星力值'
                        ],
                    ];
                    $right_list[$i] = $item;
                }

                //vip等级所需的星力值
                $vip_star_list = [];
                foreach ($vips as $k => $v)
                {
                    $vip_star_list[$v['level']] = $v['star_num'];
                }
                $data = [
                    'user' => $user,
                    'vip_star_list' => $vip_star_list,
                    'jobs' => $jobs,
                    'tasks' => $tasks,
                    'coupon_list' => $this->vipCouponList(),
                    'right_list' => $right_list
                ];
                $tmpl = 'vip_center';
            } else
            {
                //获取开通的vip用户列表20条 若需要保持及时性，请在 会员-支付成功通知，删除缓存(user:lately_vip_users:all)
                $hint_list = [];
                $list = (new ZlUserVip())->getLatelyVipUsersFromCache();
                $user_model = new User();
                foreach ($list as $k => $v)
                {
                    try
                    {
                        $user_tmp = $user_model->getUserinfo(['id' => $v['id']]);
                    }
                    catch (\Exception $e)
                    {
                        continue;
                    }
                    //头像，如果是https 开头的

                    $avatar = $user_tmp['avatar'];
                    $preg = "/^http(s)?:\\/\\/.+/";
                    if (!preg_match($preg, $avatar))
                    {
                        $avatar_tmp = substr($avatar, 0, 1);
                        if ($avatar_tmp === '/')
                        {
                            $user_tmp['avatar'] = $this->oss . substr($avatar, 1, strlen($avatar));
                        } else
                        {
                            $user_tmp['avatar'] = $this->oss . $avatar;
                        }

                    }

                    //***开通了小行星会员，没有字符三个*，一个字符补两个*，两个字符补1个*，三个字符，替换最后一个*；昵称超过3个字符的话，截取3个字符，加一个*
                    $nickname = $user_tmp['nickname'];
                    //去掉所有换行符
                    $nickname = str_replace(["\r\n", "\r", "\n"], "", $nickname);
                    $count_nickname = mb_strlen($nickname);
                    if ($nickname === '')
                    {
                        $nickname = '***';
                    } else if ($count_nickname === 1)
                    {
                        $nickname .= '**';
                    } else if ($count_nickname === 2)
                    {
                        $nickname .= '*';
                    } else if ($count_nickname >= 3)
                    {
                        $nickname = mb_substr($nickname, 0, 2) . '*';
                    }

                    $hint_list[] = [
                        'nickname' => $nickname,
                        'avatar' => $user_tmp['avatar'],
                    ];
                }
                $next_vip_level_star_num = 0;
                if ($user['vip'] !== 8)
                {
                    $next_vip_level_star_num = count($vips) > 0 ? $vips[$user['vip']]['star_num'] : '';
                }
                if (Util::IsIos() && Util::CheckAppVersion('2.9.7') === '大')
                {
                    $goodList = [
                        ['time' => '1个月', 'price' => '26', 'des' => '￥26元/月'],
                        ['time' => '3个月', 'price' => '65', 'des' => '￥22元/月'],
                        ['time' => '6个月', 'price' => '117', 'des' => '￥20元/月'],
                        ['time' => '12个月', 'price' => '175', 'des' => '超值15元/月'],
                    ];
                    $appleStatus = (int)($userVip['apple_status'] ?? 0);
                    if ($appleStatus !== 1 && Util::CheckAppVersion('2.9.10') !== '小')
                    {
                        array_unshift($goodList, ['time' => '连续包年', 'price' => '149', 'des' => '原价175元']);
                        array_unshift($goodList, ['time' => '连续包月', 'price' => '19.9', 'des' => '原价26元']);
                    }
                } else
                {

                    $goodList = [
                        ['time' => '1个月', 'price' => '18', 'des' => '￥18元/月'],
                        ['time' => '3个月', 'price' => '45', 'des' => '￥15元/月'],
                        ['time' => '6个月', 'price' => '80', 'des' => '￥14元/月'],
                        ['time' => '12个月', 'price' => '120', 'des' => '超值10元/月'],
                    ];

//                    if ($isAppleCheck)
//                    {
//                        $goodList = [
//                            ['time' => '1个月', 'price' => '18', 'des' => '￥18元/月'],
//                            ['time' => '3个月', 'price' => '45', 'des' => '￥15元/月'],
//                            ['time' => '6个月', 'price' => '78', 'des' => '￥13元/月'],
//                            ['time' => '12个月', 'price' => '118', 'des' => '超值10元/月'],
//                        ];
//                    } else
//                    {
//                        $goodList = [
//                            ['time' => '1个月', 'price' => '18', 'des' => '￥18元/月'],
//                            ['time' => '3个月', 'price' => '45', 'des' => '￥15元/月'],
//                            ['time' => '6个月', 'price' => '80', 'des' => '￥14元/月'],
//                            ['time' => '12个月', 'price' => '120', 'des' => '超值10元/月'],
//                        ];
//                    }
                }
                $data = [
                    'user' => $user,
                    'hint_list' => $hint_list,
                    'goods_list' => $goodList,
                    //只有会员过期(status=2)的有此字段
                    'progress' => [
                        'next_vip_level_star_num' => $next_vip_level_star_num,
                        'vip' => $vip + 1,
                    ],
                    //只有会员未订购(status=0)的有此字段
                    'alert' => [
                        'list' => [
                            [
                                'title' => '每日运势年可省',
                                'price' => '240'
                            ],
                            [
                                'title' => '月度运势年可省',
                                'price' => '240'
                            ],
                            [
                                'title' => '年度运势年可省',
                                'price' => '120'
                            ],
                            [
                                'title' => '幸运之城年可省',
                                'price' => '120'
                            ],
                        ],
                        'total_price' => '720'
                    ],
                    'right_list' => [
                        [
                            'icon' => 'icon_vip.png',
                            'title' => '专属身份铭牌',
                        ],
                        [
                            'icon' => 'icon_day.png',
                            'title' => '日运免费',
                        ],
                        [
                            'icon' => 'icon_month.png',
                            'title' => '月运折扣/免费',
                        ],
                        [
                            'icon' => 'icon_year.png',
                            'title' => '年运折扣',
                        ],
                        [
                            'icon' => 'icon_map.png',
                            'title' => '幸运之城折扣',
                        ],
                        [
                            'icon' => 'icon_star.png',
                            'title' => '星力值成长加速',
                        ],
                        [
                            'icon' => 'icon_more.png',
                            'title' => '权益持续更新',
                        ],

                    ]
                ];

                //判断版本
                $version_compare = Util::CheckAppVersion('2.3.0');
                $version_compare === '大' ? $vip_boon = 7 : $vip_boon = 6;
                if ($version_compare === '大')
                {
                    $data['right_list'][] = [
                        'icon' => 'icon_mall.png',
                        'title' => '万幸商城专享价',
                    ];
                    $temp = $data['right_list'][6];
                    $data['right_list'][6] = $data['right_list'][7];
                    $data['right_list'][7] = $temp;
                    unset($temp);
                }
                $tmpl = 'vip_center_not';
            }
            if (Util::CheckAppVersion('2.10.0') !== '小')
            {
                $tmplRes = 'vip4/' . $tmpl;
                if (Util::IsIos())
                {
                    $headerVersion = request()->header('version');
                    $subscribeAgrUrl = '/web/Agreement/subscribe';
                    $privateAgreementUrl = '/web/privacy.html?v=' . $headerVersion;
                } else
                {
                    $subscribeAgrUrl = '/web/Agreement/vip';
                }
                $vipEquity = $this->vipCenterEquity();
            } else
            {
                if (Util::CheckAppVersion('2.6.0') !== '小')
                {
                    $tmplRes = 'vip2/' . $tmpl;
                    if (Util::CheckAppVersion('2.9.10') !== '小')
                    {
                        if (Util::IsIos())
                        {
                            $tmplRes = 'vip3/' . $tmpl;
                            $headerVersion = request()->header('version');
                            $subscribeAgrUrl = '/web/Agreement/subscribe';
                            $privateAgreementUrl = '/web/privacy.html?v=' . $headerVersion;
                        } else
                        {
                            $subscribeAgrUrl = '/web/Agreement/vip';
                        }
                    }
                }
            }
            $tmpl = $tmplRes ?? $tmpl;
            return $this->fetch(
                $tmpl,
                [
                    'h_of_android_status_bar' => $h_of_android_status_bar,
                    'oss' => $this->oss,
                    'domain' => $this->domain,
                    'app_type' => $app_type,
                    'data' => $data,
                    'is_all_screen' => $isAllScreen,
                    'vip_boon' => $vip_boon ?? 6,
                    'sub_agr_url' => $subscribeAgrUrl ?? '',
                    'private_agreement_url' => $privateAgreementUrl ?? '',
                    'apple_status' => $userVip['apple_status'] ?? 0,
                    'is_ios' => Util::IsIos(),
                    'vip_equity' => $vipEquity ?? []
                ]
            );
        }
        catch (\Exception $e)
        {
            throw new ApiException('[' . $e->getFile() . ':' . $e->getLine() . ']' . PHP_EOL . 'vip中心h5，错误：' . $e->getMessage());
        }
    }

    /**
     * 会员权益说明
     * @return mixed
     * @throws \Exception
     */
    public function vip_right()
    {
        //ios是否是全面屏，0：不是；1：是；
        $index = input('index/d', 0);
        $data = $this->getVipRight();
        if (Util::CheckAppVersion('2.10.0') !== '小')
        {
            if (Util::CheckAppVersion('2.10.5') !== '小')
            {
                unset($data[2], $data[3]);
                //全部权益的位置定位
                if (Util::checkAppVersion('2.14.0') !== '小')
                {
                    $desiredOrder = [9, 10, 13, 15, 14, 12, 11, 8, 4, 5, 1, 6, 7];
                } elseif (Util::checkAppVersion('2.13.0') !== '小')
                {
                    $desiredOrder = [9, 10, 13, 14, 12, 11, 8, 4, 5, 1, 6, 7];
                } elseif (Util::checkAppVersion('2.12.0') !== '小')
                {
                    $desiredOrder = [9, 10, 12, 11, 8, 4, 5, 1, 6, 7];
                } else
                {
                    $desiredOrder = [9, 10, 11, 8, 4, 5, 1, 6, 7];
                }

                // 定义目标排序顺序
                uksort($data, static function ($a, $b) use ($desiredOrder)
                {
                    $pos_a = array_search($a, $desiredOrder, true);
                    $pos_b = array_search($b, $desiredOrder, true);

                    // 如果两者都在目标顺序中，则按目标顺序比较位置
                    if ($pos_a !== false && $pos_b !== false)
                    {
                        return $pos_a - $pos_b;
                    }
                    // 如果其中一个不在目标顺序中，保持其原有顺序不变
                    if ($pos_a === false)
                    {
                        return 1;
                    }
                    if ($pos_b === false)
                    {
                        return -1;
                    }
                    // 不应到达此处，但以防万一，返回相等
                    return 0;
                });
            }
            return $this->fetch('vip4/vip_right', ['data' => array_values($data), 'oss' => $this->oss, 'index' => $index]);
        }

        return $this->fetch('vip_right', ['data' => array_values($data), 'oss' => $this->oss, 'index' => $index]);
    }

    /**
     * VIP个人中心专属权益列表
     * @return array[]
     */
    private function vipCenterEquity()
    {
        if (Util::checkAppVersion('2.14.0') !== '小')
        {
            $vipEquity = [
                [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right5-2.png",
                    'index' => 0
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right6.png",
                    'index' => 1
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right7.png",
                    'index' => 5
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right1.png",
                    'index' => 6
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right3.png",
                    'index' => 9
                ],
            ];
        } elseif (Util::checkAppVersion('2.13.0') !== '小')
        {
            $vipEquity = [
                [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right5-2.png",
                    'index' => 0
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right6.png",
                    'index' => 1
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right7.png",
                    'index' => 4
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right1.png",
                    'index' => 5
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right3.png",
                    'index' => 8
                ],
            ];
        } elseif (Util::CheckAppVersion('2.10.5') !== '小')
        {
            $vipEquity = [
                [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right5-2.png",
                    'index' => 0
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right6.png",
                    'index' => 1
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right7.png",
                    'index' => 2
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right1.png",
                    'index' => 3
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right3.png",
                    'index' => 6
                ],
            ];
        } elseif (Util::CheckAppVersion('2.10.2') !== '小')
        {
            $vipEquity = [
                [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right5.png",
                    'index' => 0
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right1.png",
                    'index' => 1
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right2.png",
                    'index' => 3
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right3.png",
                    'index' => 2
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right4.png",
                    'index' => 7
                ],
            ];
        } else
        {
            $vipEquity = [
                [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right1.png",
                    'index' => 0
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right2.png",
                    'index' => 2
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right3.png",
                    'index' => 1
                ], [
                    'icon' => $this->oss . "vip/images/vip_right/vip_right4.png",
                    'index' => 6
                ],
            ];
        }
        return $vipEquity;
    }

    /**
     * 会员全部权益信息
     * @return array[]
     * @throws \Exception
     */
    private function getVipRight()
    {
        //获取后台录入的:VIP等级/会员名称/基础星力值/成长倍速
        $vips = (new ZlVip())->getLevelAll();
        $grow_nums = [];
        foreach ($vips as $v)
        {
            $grow_nums[] = $v['grow_num'] . '倍星力值加速成长';
        }
        $data = [
            1 => [
                'id' => 1,
                'icon' => 'icon_vip.png',
                'title' => '专属身份铭牌',
                'des' => '拥有独特的身份铭牌显示，彰显您的与众不同，根据不同的VIP等级可以获得不同的铭牌展示。',
                'header' => '专属身份铭牌',
                'list' => [1, 2, 3, 4, 5, 6, 7, 8],
                'type' => 2
            ],
            2 => [
                'id' => 2,
                'icon' => 'icon_day.png',
                'title' => '日运免费券',
                'des' => '会员专享【1日】每日运势免费券，可以使用该免费券解锁每日运势，会员有效期内，免费券会每月自动放入您的【卡券中心】，有效期为一个月，当月连续升级，会员权益直接提升至您所处的级别，例如：当前会员为V1，本月直接升级至V3，即可直接享受到V3级别的会员权益。每日运势免费券可以赠与好友使用。',
                'header' => '日运免费券',
                'list' => [
                    '日运免费券 10张/月',
                    '日运免费券 11张/月',
                    '日运免费券 12张/月',
                    '日运免费券 13张/月',
                    '日运免费券 15张/月',
                    '日运免费券 17张/月',
                    '日运免费券 17张/月',
                    '日运免费券 18张/月',
                ],
                'type' => 2
            ],
            3 => [
                'id' => 3,
                'icon' => 'icon_month.png',
                'title' => '月运免费券',
                'des' => '会员专享【1个月】月度运势免费券，可以使用该免费券解锁月度运势，会员有效期内，优惠券会每月自动放入您的【卡券中心】，有效期为一个月，当月连续升级，会员权益直接提升至您所处的级别，例如：当前会员为V1，本月直接升级至V3，即可直接享受到V3级别的会员权益。月度运势免费券可以赠与好友使用。',
                'header' => '月运免费券',
                'list' => [
                    '月运五折券 1张/月',
                    '月运【一个月】免费券 1张/月',
                    '月运【一个月】免费券 1张/月',
                    '月运【一个月】免费券 1张/月',
                    '月运【一个月】免费券 1张/月',
                    '月运【一个月】免费券 1张/月',
                    '月运【一个月】免费券 1张/月',
                    '月运【一个月】免费券 1张/月',
                ],
                'type' => 2
            ],
            4 => [
                'id' => 4,
                'icon' => 'icon_year.png',
                'title' => '年运折扣券',
                'des' => '会员专享年度运势优惠券，可以使用优惠券解锁年度运势时享受一定比例的优惠，会员有效期内，优惠券会每月自动放入您的【卡券中心】，有效期为一个月，当月连续升级，会员权益直接提升至您所处的级别，例如：当前会员为V1，本月直接升级至V3，即可直接享受到V3级别的会员权益。年度运势优惠券可以赠与好友使用。',
                'header' => '年运折扣券',
                'list' => [
                    '年运9折券 1张/月',
                    '年运9折券 1张/月',
                    '年运8折券 1张/月',
                    '年运8折券 1张/月',
                    '年运8折券 1张/月',
                    '年运8折券 1张/月',
                    '年运7.5折券 1张/月',
                    '年运7折券 1张/月',
                ],
                'type' => 2
            ],
            5 => [
                'id' => 5,
                'icon' => 'icon_map.png',
                'title' => '幸运之城折扣券',
                'des' => '会员专享幸运之城优惠券，可以使用优惠券解锁幸运之城时享受一定比例的优惠，会员有效期内，优惠券会每月自动放入您的【卡券中心】，有效期为一个月，当月连续升级，会员权益直接提升至您所处的级别，例如：当前会员为V1，本月直接升级至V3，即可直接享受到V3级别的会员权益。幸运之城优惠券可以赠与好友使用。',
                'header' => '幸运之城优惠券',
                'list' => [
                    '幸运之城9折券 1张/月',
                    '幸运之城9折券 1张/月',
                    '幸运之城9折券 1张/月',
                    '幸运之城8折券 1张/月',
                    '幸运之城8折券 1张/月',
                    '幸运之城8折券 1张/月',
                    '幸运之城7.5折券 1张/月',
                    '幸运之城7折券 1张/月',
                ],
                'type' => 2
            ],
            6 => [
                'id' => 6,
                'icon' => 'icon_star.png',
                'title' => '星力值成长加速',
                'des' => '会员首次开通会员服务至少涨200星力值，并且享受加倍成长特权，除此之外会员每次续费按照等级不同享受指定星力值成长。',
                'header' => '星力值成长加速',
                'list' => $grow_nums,
                'type' => 2
            ],
        ];
        //判断版本
        if (Util::CheckAppVersion('2.3.0') === '大')
        {
            $data[7] = [
                'id' => 7,
                'icon' => 'icon_mall.png',
                'title' => '万幸商城专享价',
                'des' => '开通会员权益后，所有等级的会员均可在商城中消费对应的商品时享受相应的会员专属折扣价，此外还有更多会员专享福利及好礼持续更新中。',
                'header' => '万幸商城专享价',
                'list' => [
                    '限定商品会员低价购',
                    '限定商品会员低价购',
                    '限定商品会员低价购',
                    '限定商品会员低价购',
                    '限定商品会员低价购',
                    '限定商品会员低价购',
                    '限定商品会员低价购',
                    '限定商品会员低价购',
                ],
                'type' => 2
            ];
        }
        $item = [];
        if (Util::checkAppVersion('2.14.0') !== '小')
        {
            $item[15] = [
                'id' => 15,
                'icon' => 'icon_peiliao.png',
                'des' => '/vip/images/vip_right/peiliao_right.png',
                'title' => 'AI星愈聊天室每日30分钟免费对话',
                'type' => 1
            ];
        }
        if (Util::checkAppVersion('2.13.0') !== '小')
        {
            $item[13] = [
                'id' => 13,
                'icon' => 'icon_synastry.png',
                'des' => '/vip/images/vip_right/synastry_right.png',
                'title' => '专属合盘工具-具体关系的合盘解读',
                'type' => 1
            ];
            $item[14] = [
                'id' => 14,
                'icon' => 'icon_acquired_astro.png',
                'des' => '/vip/images/vip_right/acquired_astro_right.png',
                'title' => '后天星座测试免费查看体验版测试结果(20道题)',
                'type' => 1
            ];
        }
//        if (Util::checkAppVersion('2.12.0') !== '小')
//        {
//            $item[12] = [
//                'id' => 12,
//                'icon' => 'icon_rtc.png',
//                'des' => '/vip/images/vip_right/rtc_right.png',
//                'title' => '会员专属特权，与“星座女神”AI分身语音通话。',
//                'type' => 1
//            ];
//        }
        if (Util::CheckAppVersion('2.10.5') !== '小')
        {
            $item[10] = [
                'id' => 10,
                'icon' => 'icon_month.png',
                'des' => '/vip/images/vip_right/month_right.png',
                'title' => '月运免费解锁',
                'type' => 1
            ];
            $item[11] = [
                'id' => 11,
                'icon' => 'icon_day.png',
                'des' => '/vip/images/vip_right/day_right.png',
                'title' => '会员期内日运免费看（当日可查看未来30天）',
                'type' => 1
            ];
            $item[9] = [
                'id' => 9,
                'icon' => 'icon_ai.png',
                'title' => 'Ai对话每日免费对话5次（骰子塔罗解答不可用）',
                'des' => '/vip/images/vip_right/ai_right2.png',
                'header' => '',
                'list' => [],
                'type' => 1
            ];
        } elseif (Util::CheckAppVersion('2.10.2') !== '小')
        {
            $item[9] = [
                'id' => 9,
                'icon' => 'icon_ai.png',
                'title' => 'Ai对话每日免费对话3次（骰子塔罗解答不可用）',
                'des' => '/vip/images/vip_right/ai_right.png',
                'header' => '',
                'list' => [],
                'type' => 1
            ];
        }

        if (Util::CheckAppVersion('2.10.0') !== '小')
        {
            $item[8] = [
                'id' => 8,
                'icon' => 'icon_meditation.png',
                'title' => '会员冥想课程全解锁',
                'des' => '/vip/images/vip_right/meditation_right.png',
                'header' => '',
                'list' => [],
                'type' => 1
            ];
            // 向数组 首部追加
            $data = $item + $data;
        }
        return $data;
    }


    /**
     * 获取vip不同等级的优惠券福利
     * @return array[]
     */
    private function vipCouponList()
    {
        if (Util::CheckAppVersion('2.10.5') !== '小')
        {
            $data = [
                1 => [
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '九折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '九折券'
                    ],
                ],
                2 => [
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '九折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '九折券'
                    ],
                ],
                3 => [
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '九折券'
                    ],
                ],
                4 => [
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                ],
                5 => [
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                ],
                6 => [
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                ],
                7 => [
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '七五折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '七五折券'
                    ],
                ],
                8 => [
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '七折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '七折券'
                    ],
                ],
            ];
        } else
        {
            $data = [
                1 => [
                    [
                        'name' => '日运券',
                        'count' => '10',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '月运券',
                        'count' => '1',
                        'des' => '五折券'
                    ],
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '九折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '九折券'
                    ],
                ],
                2 => [
                    [
                        'name' => '日运券',
                        'count' => '11',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '月运券',
                        'count' => '1',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '九折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '九折券'
                    ],
                ],
                3 => [
                    [
                        'name' => '日运券',
                        'count' => '12',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '月运券',
                        'count' => '1',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '九折券'
                    ],
                ],
                4 => [
                    [
                        'name' => '日运券',
                        'count' => '13',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '月运券',
                        'count' => '1',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                ],
                5 => [
                    [
                        'name' => '日运券',
                        'count' => '15',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '月运券',
                        'count' => '1',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                ],
                6 => [
                    [
                        'name' => '日运券',
                        'count' => '17',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '月运券',
                        'count' => '1',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '八折券'
                    ],
                ],
                7 => [
                    [
                        'name' => '日运券',
                        'count' => '17',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '月运券',
                        'count' => '1',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '七五折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '七五折券'
                    ],
                ],
                8 => [
                    [
                        'name' => '日运券',
                        'count' => '18',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '月运券',
                        'count' => '1',
                        'des' => '免费券'
                    ],
                    [
                        'name' => '年运券',
                        'count' => '1',
                        'des' => '七折券'
                    ],
                    [
                        'name' => '幸运之城券',
                        'count' => '1',
                        'des' => '七折券'
                    ],
                ],
            ];
        }
        return $data;
    }

    /**
     * 常见问题列表
     * @return mixed
     * @throws \Exception
     */
    public function vip_qa_list()
    {
        $headers = request()->header();
        $app_type = $headers['app-type'];
        $qa_list = (new ZlCommonProblems())->getCommonProblemList();
        return $this->fetch('vip_qa_list', ['oss' => $this->oss, 'qa_list' => $qa_list, 'domain' => $this->domain, 'app_type' => $app_type]);
    }

    /**
     * 常见问题详情
     * @return mixed
     * @throws \Exception
     */
    public function vip_qa_detail()
    {
        $id = input('id');
        $qa_detail = (new ZlCommonProblems())->getCommonProblemDetail($id);
        return $this->fetch('vip_qa_detail', ['oss' => $this->oss, 'data' => $qa_detail]);
    }

    /**
     * 会员升级规则
     * @return mixed
     */
    public function vip_upgrade_rule()
    {
        //oss 地址
        $img = $this->oss . 'vip/images/vip_upgrade_rule.png?v=2019.11.08';
        $title = '升级规则说明';
        return $this->fetch('common_img', ['img' => $img, 'title' => $title]);
    }

    /**
     * 星力值排行榜说明
     * @return mixed
     */
    public function start_rank_detail()
    {
        //oss 地址
        $img = $this->oss . 'vip/images/start_rank_detail.png';
        $title = '规则说明';
        return $this->fetch('common_img', ['img' => $img, 'title' => $title]);
    }

    /**
     * VIP活动banner
     * @return mixed
     */
    public function bannerActive()
    {
        $headers = $this->request->header();

        unset($headers['cookie'], $headers['time'], $headers['content-type'], $headers['content-length']);

        $headers = json_encode($headers);
        $m = Env::get('host.m');
        $this->assign('headers', $headers);
        $this->assign('m', $m);
        $this->assign('oss', $this->oss);
        return $this->fetch();
    }
}
